package com.ithiema.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 字段自动填充器
 */
    @Slf4j
    @Component
    public class MyMetaObjectHandler implements MetaObjectHandler {

        @Override
        public void insertFill(MetaObject metaObject) {
            log.info("start insert fill ....");
            log.info("当前线程的id:{}",Thread.currentThread().getId());
            //设置指定字段数据
            metaObject.setValue("createTime", LocalDateTime.now());
            metaObject.setValue("updateTime", LocalDateTime.now());
            //从session中获取当前操作用户的id
            metaObject.setValue("createUser", ApplicationContext.getData());
            metaObject.setValue("updateUser", ApplicationContext.getData());

        }

        @Override
        public void updateFill(MetaObject metaObject) {
            log.info("start update fill ....");
            //设置指定字段数据
            metaObject.setValue("updateTime", LocalDateTime.now());
            //从session中获取当前操作用户的id
            metaObject.setValue("updateUser", ApplicationContext.getData());

        }
    }

